class Solution:
    def scheduleCourse(self, courses: List[List[int]]) -> int:
        courses.sort(key=lambda c:c[1])

        q=list()
        ans=0

        for ti, di in courses:
            if ans + ti <= di:
                ans += ti
                heapq.heappush(q, -ti)
            elif q and -q[0] > ti:
                ans -= -q[0] - ti
                heapq.heappop(q)
                heapq.heappush(q, -ti)

        return len(q)